using System.ServiceModel;

namespace SharpTalk.Core.Services.ServiceAgents
{
    using Requests;
    using Responses;
    using System;

    public sealed class ChatRoomServicesAgent
         : ServiceAgentBase<
        Contracts.Server.IChatRoomServices,
        ChatRoomCreateRequest, ChatRoomCreateResponse,
        ChatRoomFindRequest, ChatRoomFindResponse,
        ChatRoomUpdateRequest, ChatRoomUpdateResponse,
        ChatRoomDeleteRequest, ChatRoomDeleteResponse>,
        Contracts.Server.IChatRoomServices
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Services.ServiceAgents.ChatRoomServicesAgent"/> class using the configuration information specified in the application configuration file by <paramref name="endpointConfigurationName"/>. 
        /// </summary>
        /// <param name="endpointConfigurationName">The name of the endpoint in the application configuration file.</param><exception cref="T:System.ArgumentNullException">The specified endpoint information is null.</exception><exception cref="T:System.InvalidOperationException">The endpoint cannot be found or the endpoint contract is not valid.</exception>
        public ChatRoomServicesAgent(string endpointConfigurationName) 
            : base(endpointConfigurationName)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Services.ServiceAgents.ChatRoomServicesAgent"/> class using the configuration information specified in the application configuration file by <paramref name="endpointConfigurationName"/>. 
        /// </summary>
        /// <param name="endpointConfigurationName">The name of the endpoint in the application configuration file.</param>
        /// <param name="remoteAddress"></param>
        /// <exception cref="T:System.ArgumentNullException">The specified endpoint information is null.</exception><exception cref="T:System.InvalidOperationException">The endpoint cannot be found or the endpoint contract is not valid.</exception>
        public ChatRoomServicesAgent(string endpointConfigurationName, EndpointAddress remoteAddress) 
            : base(endpointConfigurationName, remoteAddress)
        {
        }

        public IAsyncResult BeginJoinChatRoom(ChatRoomJoinRequest request, AsyncCallback callback, object state)
        {
            return base.Channel.BeginJoinChatRoom(request, callback, state);
        }

        public ChatRoomJoinResponse EndJoinChatRoom(IAsyncResult result)
        {
            try
            {
                return base.Channel.EndJoinChatRoom(result);
            }
            catch (Exception exp)
            {
                return new ChatRoomJoinResponse(){Error = new ErrorDetails(exp)};
            }
        }

        public IAsyncResult BeginLeaveChatRoom(ChatRoomLeaveRequest request, AsyncCallback callback, object state)
        {
            return base.Channel.BeginLeaveChatRoom(request, callback, state);
        }

        public ChatRoomLeaveResponse EndLeaveChatRoom(IAsyncResult result)
        {
            try
            {
                return base.Channel.EndLeaveChatRoom(result);
            }
            catch (Exception exp)
            {
                return new ChatRoomLeaveResponse(){Error = new ErrorDetails(exp)};
            }
        }

        public IAsyncResult BeginGetUserList(ChatRoomUserListRequest request, AsyncCallback callback, object state)
        {
            return base.Channel.BeginGetUserList(request, callback, state);
        }

        public ChatRoomUserListResponse EndGetUserList(IAsyncResult result)
        {
            try
            {
                return base.Channel.EndGetUserList(result);
            }
            catch (Exception exp)
            {
                return new ChatRoomUserListResponse(){ Error = new ErrorDetails(exp) };
            }
        }
    }
}